package com.tl.khadmin.service.impl;
 
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;
import javax.servlet.http.HttpSession;

import org.springframework.stereotype.Service;

import com.tl.khadmin.bean.Staff;
import com.tl.khadmin.bean.Supplier;
import com.tl.khadmin.bean.Warehouse; 
import com.tl.khadmin.mapper.WarehouseMapper; 
import com.tl.khadmin.page.Page;
import com.tl.khadmin.service.WareHouseIService; 
import com.tl.khadmin.util.CommonUtil;
import com.tl.khadmin.util.Constant; 

@Service
public class WareHouseServiceImpl implements WareHouseIService{
	
	@Resource
	private WarehouseMapper warehouseMapper;
	
	public List<Warehouse> listWarehouse(HttpSession session, Map<String, Object> param){
		Supplier supplier = (Supplier) session.getAttribute(Constant.USER_SUPPLIER);
		param.put("supplierId", supplier.getId());
		return warehouseMapper.listWarehouse(param); 
	}
	
	@Override
	public String updateWarehouse(Warehouse warehouse) {
		Integer id = warehouseMapper.queryIdByNameAndId(warehouse.getId(), warehouse.getWarehouseName());
		if (id != null) {
			return CommonUtil.returnMapResultToJson("fail","品牌名称已经存在");
		}  
		int result = warehouseMapper.updateByPrimaryKeySelective(warehouse);
		return CommonUtil.returnMapResultByRows(result, "修改成功", "修改失败，请重试");
	}
	
	@Override
	public Page<Warehouse> showWarehouseByPage(HttpSession session, Integer pageNum, 
			Integer pageSize, Map<String, Object> params) {
		Supplier supplier = (Supplier) session.getAttribute(Constant.USER_SUPPLIER);
		params.put("supplierId", supplier.getId());
		Page<Warehouse> page=new Page<Warehouse>(pageNum, pageSize);
        params.put("page", page);   
        List<Warehouse> list= warehouseMapper.showWarehouseByPage(params);  
		page.setResult(list);
		return page;
	}

	@Override
	public String saveWarehouse(HttpSession session, Warehouse warehouse) {
		Integer id = warehouseMapper.queryIdByName(warehouse.getWarehouseName());
		if (id != null) {
			return CommonUtil.returnMapResultToJson("fail","品牌名称已经存在");
		} 
		Staff staff = (Staff) session.getAttribute(Constant.USER);
		Supplier supplier = (Supplier) session.getAttribute(Constant.USER_SUPPLIER); 
		warehouse.setCreateId(staff.getId().longValue());
		warehouse.setSupplierId(supplier.getId());
		warehouse.setCreateTime(new Date()); 
		warehouse.setSupplierId(staff.getSupplierId());
		int result = warehouseMapper.insertSelective(warehouse);
		return CommonUtil.returnMapResultByRows(result, "添加成功", "添加失败，请重试");
	}
 
	@Override
	public String deleteByIds(String ids) {
		String[] idsArr = ids.substring(0,ids.length()-1).split(",");
		try{
			int result = warehouseMapper.deleteByIds(idsArr);
		}catch(Exception ex) {
			return CommonUtil.returnMapResultByRows(0, "删除成功","数据不能删除");
		} 
		return CommonUtil.returnMapResultByRows(1, "删除成功","删除失败，请重试");
	}

	@Override
	public Map<String, Object> queryWarehouseById(Integer id) {
		Map<String, Object> result = new HashMap<>();
		Warehouse warehouse = warehouseMapper.selectByPrimaryKey(id); 
		result.put("warehouse", warehouse); 
		return result;
	} 

}
